package com.ukao.steward.dialog;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.adapter.ServeTagAdapter;
import com.ukao.steward.base.BaseDialogFragment;
import com.ukao.steward.bean.ShoppingBean;
import com.ukao.steward.eventbus.ShoppEvent;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.ImageUtils;
import com.ukao.steward.widget.TagFlowLayout;

import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;





/**
 * 选择服务
 */
public class ServeCarDialogFragment extends BaseDialogFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.imgPath)
    ImageView imgPath;
    @BindView(R.id.product_name)
    TextView name;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.close_btn)
    ImageView closeBtn;
    @BindView(R.id.id_tagflowlayout)
    TagFlowLayout idTagflowlayout;
    @BindView(R.id.subtract_btn)
    Button subtractBtn;
    @BindView(R.id.count)
    TextView count;
    @BindView(R.id.add_btn)
    Button addBtn;
    Unbinder unbinder;
    @BindView(R.id.commit_btn)
    Button commitBtn;
    public String serverId;
    public String serverName;
    private String mParam1;
    private String mParam2;
    private ServeTagAdapter mServeTagAdapter;
    private ShoppingBean mShoppingBean;
    private int defaultpos;
    private int position;
    private int servicePrice;
    public int serverPice;

    public static ServeCarDialogFragment newInstance(ShoppingBean mitem , int position) {
        ServeCarDialogFragment fragment = new ServeCarDialogFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, mitem);
        args.putInt(ARG_PARAM2, position);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mShoppingBean = getArguments().getParcelable(ARG_PARAM1);
            position= getArguments().getInt(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_serve_car_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }

    @Override
    protected void initView() {
        setShowBottom(true);
        setIsWithWrap(false);
        name.setText(mShoppingBean.getName());

        ImageUtils.loadImage(activity,mShoppingBean.getImgPath(),imgPath);
        if (mShoppingBean.getCount()==0){
            count.setText(1+"");
        }else{
            count.setText(mShoppingBean.getCount()+"");
        }

        List<ShoppingBean.ServiceListBean> serviceList = mShoppingBean.getServiceList();
        mServeTagAdapter= new ServeTagAdapter(activity,serviceList );
       idTagflowlayout.setAdapter(mServeTagAdapter);
        price.setText("￥"+CheckUtils.isEmptyNumber(mShoppingBean.getPrice()));
        serverName=mShoppingBean.getSelectServiceName();
        serverId=mShoppingBean.getSelectServiceId();
        defaultpos=0;

        if (!CheckUtils.isEmpty(serverName)){
            for (int i = 0; i < serviceList.size(); i++) {
                ShoppingBean.ServiceListBean serviceListBean = serviceList.get(i);
                if (serverName.equals(serviceListBean.getName())&&serverId.equals(serviceListBean.getId())) {
                    defaultpos=i;
                    serverPice=serviceListBean.getPrice();
                    price.setText("￥"+CheckUtils.isEmptyNumber(serverPice));
                    mServeTagAdapter.setSelectedList(defaultpos);
                    commitBtn.setEnabled(true);
                    commitBtn.setAlpha(1);
                }
            }

        }

    }

    @Override
    protected void initListener() {
        super.initListener();
        idTagflowlayout.setOnSelectListener(mOnSelectListener);

    }



    private TagFlowLayout.OnSelectListener mOnSelectListener = new TagFlowLayout.OnSelectListener() {




        @Override
        public void onSelected(Set<Integer> selectPosSet) {
            if (CheckUtils.isEmpty(selectPosSet))return;
            Integer selectpos=0;
            for (Integer pos :
                    selectPosSet) {
                selectpos = pos;
            }
            ShoppingBean.ServiceListBean item = mServeTagAdapter.getItem(selectpos);
            if (CheckUtils.isNull(item))return;
            serverPice=item.getPrice();
            price.setText("￥"+CheckUtils.isEmptyNumber(serverPice));
        if (!commitBtn.isEnabled()){
            commitBtn.setEnabled(true);
            commitBtn.setAlpha(1);
        }


        }
    };
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.close_btn, R.id.subtract_btn, R.id.add_btn, R.id.commit_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                dismiss();
                break;
            case R.id.subtract_btn:/*减去*/
                if (!CheckUtils.isEmpty(getText(count))) {
                    int clothingCount = Integer.valueOf(getText(count));
                    if (clothingCount > 0) {
                        clothingCount = clothingCount - 1;
                        String strclothingCount = String.valueOf(clothingCount);
                        count.setText(strclothingCount);

                    }
                }


                break;

            case R.id.add_btn:/*加*/
                String strcount = getText(count);
                if (!CheckUtils.isEmpty(strcount)) {
                    int clothingCount1 = Integer.valueOf(strcount);
                    clothingCount1 = clothingCount1 + 1;
                    String strclothingCount1 = String.valueOf(clothingCount1);
                    count.setText(strclothingCount1);
                } else {
                    count.setText("1");
                }
                break;


            case  R.id.commit_btn:
                for (Integer integer : idTagflowlayout.getSelectedList()) {
                    ShoppingBean.ServiceListBean item = mServeTagAdapter.getItem(integer);
                    if (!CheckUtils.isNull(item)){
                        mShoppingBean.setSelectServiceName(item.getName());
                        mShoppingBean.setSelectServiceId(item.getId());
                        mShoppingBean.setPrice(item.getPrice());
                    }

                }

                 int   intcount = Integer.valueOf(getText(count));
                  mShoppingBean.setCount(intcount);
                ShoppEvent.postHasData(ShoppEvent.Message.CHAGE_ADD_COUNT,mShoppingBean);
                if (mDialogSubmitListener!=null){
                    mDialogSubmitListener.onSubmitClick(R.id.commit_btn,position);
                }
                dismiss();
                break;
        }
    }
}
